package com.dingding.service.account.core.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.beans.special.Specialaccounttradedetail;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.service.account.core.dao.IUserAccountDao;
import com.dingding.service.account.core.dao.IUserAccountDetailDao;
import com.dingding.service.account.core.service.IUserAccountService;

/**
 * 
 * @ClassName: UserAccountServiceImpl 
 * @Description: 用户账户业务操作
 * @author CHEHAUNBO 
 * @date 2016年7月11日 下午5:42:42
 * 
 */
@Component("userAccountSer")
public class UserAccountServiceImpl implements IUserAccountService {

	private Logger log = Logger.getLogger(UserAccountServiceImpl.class);

	/**
	 * 用户账户操作DAO
	 * @author CHEHUANBO
	 */
	@Resource(name = "userAccountDao")
	private IUserAccountDao userAccountDao;

	/**
	 * 用户账户明细DAO
	 * @author CHEHUANBO
	 */
	@Resource(name = "userAccountDetailDao")
	private IUserAccountDetailDao userAccountDetailDao;

	/**
	 * 
	 * @Description: 扣钱
	 * @param userId 用户ID
	 * @param money 金额
	 * @author CHEHUANBO
	 * @return boolean true 扣除成功   false 扣除失败
	 * @since V2.3.0
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public boolean deductRemainMoney(String userId, BigDecimal money) {

		try {
			userAccountDao.deductRemainMoney(userId, money);
			return true;
		} catch (Exception e) {
			log.error("扣除用户金额出现异常！", e);
		}

		return false;

	}
	
	/**
	 * 
	 * @Description: 扣钱（出租车）
	 * @param userId 用户ID
	 * @param money 金额
	 * @author YHQ
	 * @since V2.5.4
	 * @return boolean true 扣除成功   false 扣除失败
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public boolean taxiDeductRemainMoney(String userId, BigDecimal money) {

		try {
			userAccountDao.taxiDeductRemainMoney(userId, money);
			return true;
		} catch (Exception e) {
			log.error("扣除用户金额出现异常！", e);
		}

		return false;

	}
	

	/**
	 * 
	 * @Title: createUserAccountDetail 
	 * @param userId 用户ID
	 * @param tradeMoney  交易金额
	 * @param tradeType  交易类型
	 * @param actualPaymentMoney 实付金额
	 * @author CHEHAUNBO
	 * 
	 */
	private Specialaccounttradedetail createUserAccountDetail(String userId, BigDecimal tradeMoney, Integer tradeType) {

		Specialaccounttradedetail useraccountdetail = new Specialaccounttradedetail();

		useraccountdetail.setUserId(userId);// 用户ID
		useraccountdetail.setSatId(ToolUtil.uuid()); // 主键ID
		useraccountdetail.setTradeType(tradeType); // 交易类型

		useraccountdetail.setTradeMoney(tradeMoney); // 交易金额
		useraccountdetail.setPreAmount(new BigDecimal(0));
		useraccountdetail.setAftAmount(new BigDecimal(0));
		useraccountdetail.setTradeTime(new Date()); // 交易时间
		return useraccountdetail;

	}

	/**
	 * 
	 * @Description: 充值
	 * @param userId
	 *            用户ID
	 * @param money
	 *            金额
	 * @author CHEHUANBO
	 * @return boolean true 扣除成功 false 扣除失败
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public boolean recharge(String userId, BigDecimal money) {

		try {
			userAccountDao.specialRecharge(userId, money);
			userAccountDetailDao.addSpecialAccountDetail(createUserAccountDetail(userId, money, 1));
			return true;
		} catch (Exception e) {
			log.error("用户充值出现异常！", e);
		}

		return false;
	}

	/**
	 * 
	 * @Title: refunds 
	 * @Description: 给乘客退款(顺风车) 
	 * @param loginId 用户Id
	 * @param fee 退款金额
	 * @author YHQ 
	 * @since V2.5.4
	 * @return boolean    返回类型
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public boolean refunds(String loginId, BigDecimal fee){
		
		return userAccountDao.refunds(loginId, fee);
	}
	
	
	/**
	 * 
	 * @Title: transferAccounts 
	 * @Description: 转账操作
	 * @param loginId 用户Id
	 * @param fee 转账金额
	 * @author YHQ 
	 * @since V2.5.4
	 * @return boolean    返回类型
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public boolean transferAccounts(String loginId, BigDecimal fee){
		
		return userAccountDao.transferAccounts(loginId, fee);
	}
	
}
